<?php
	require 'libraries/video/video.php';
	class youtube extends video{
		
		protected $_domain = 'youtube.com';
		public	  $_alias = 'youtube';
		protected $_requestVideoUrl = 'https://www.googleapis.com/youtube/v3/videos?';
		protected $_requestPlaylistUrl = 'https://www.googleapis.com/youtube/v3/playlists?';
		protected $_apiKey = 'AIzaSyAQesqxLMheTrLVBUAFGCzLlq8mok0y--0';
		protected $_embedCode = '';
		protected $_content;
		protected $_thumbSizeImage = array(
					'standard',
					'medium',
					'high',
					'default'
				);
		
		
		
		public function getVideoInformation($queryParams, $options = array()){
			$countParam = count($queryParams);

			if($countParam <= 0 || !$queryParams['v']) {
				$this->_errors = 'Đường dẫn video không chính xác.';
				return false;
			}
				
			$additionQuery = 'id=' . $queryParams['v'] . '&part=snippet';
			
			$content = $this->getRequest($this->_requestVideoUrl . $additionQuery);

			if(!$content){

				$this->_errors = 'Không tồn tại video này. Xin vui lòng thử lại.';
			
			}else{
				
				if($queryParams['list']){
					$additionListQuery = 'id=' . $queryParams['list'] . '&part=snippet';
					$listContent =  $this->getRequest($this->_requestPlaylistUrl . $additionListQuery);
					if(!$listContent){
						$this->_errors = 'Playlist không tồn tại. Xin vui lòng thử lại.';
					}else{
						$this->setEmbedCode($queryParams, true);
						$this->setContent($content);
					}
				}else{
					$this->setEmbedCode($queryParams, false);
					$this->setContent($content);
				}
			}
		}
		
		public function setContent($content){
			$this->_content = $content;
		}
		
		public function getContent(){
			return $this->_content;
		}
		
		/*
		 *  Set embed code
		*  @params array $queryParams
		*  return embed code iframe
		*/
		public function setEmbedCode( $params,  $list = false){
			if($_SESSION['mobile'] == 0) { $height = '360'; }else{ $height = '315';}
			if($list == false){
				
				$this->_embedCode = '<iframe id="ytvideo_'.$params['v'].'" width="100%" class="embed_video youtube_video" height="350" src="//www.youtube.com/embed/'.$params['v'].'?enablejsapi=1" frameborder="0" allowfullscreen></iframe>';
			}else{
				$this->_embedCode = '<iframe id="ytvideo_'.$params['v'].'" width="100%" class="embed_video youtube_video" height="350" src="//www.youtube.com/embed/'.$params['v'].'?list='.$params['list'].'&enablejsapi=1" frameborder="0" allowfullscreen></iframe>';
			}
			
			/*
			 *  <iframe width="560" height="315" src="//www.youtube.com/embed/Glik7RAD4bE" frameborder="0" allowfullscreen></iframe>
			*  <iframe width="560" height="315" src="//www.youtube.com/embed/GNb8T5NBdQg?list=PL70221A06CF173BB4" frameborder="0" allowfullscreen></iframe>
			*  <iframe width="560" height="315" src="//www.youtube.com/embed/okYS3QyJS-4?list=UUP8hPwkqzWMjQMiPD8wk_aQ" frameborder="0" allowfullscreen></iframe>
			*/
				
				
		}
		
		public function getEmbedCode(){
			return $this->_embedCode;
		}
		
		public function getImage($linkImage){
	
			foreach($this->_thumbSizeImage as $value){
				if(isset($linkImage[$value]['url'])){
					return $linkImage[$value]['url'];
				}
			}
			return false;
		}
		

		
		public function getRequest($url){
			
			$ch = curl_init();
			// Set query data here with the URL
			curl_setopt($ch, CURLOPT_URL,  $url . '&key=' . $this->_apiKey);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_TIMEOUT, '10');
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			$content = trim(curl_exec($ch));
			curl_close($ch);
			
			if($content){
				$content = json_decode($content,true);
			}
			
			if(!$content['items']){
				return false;
			}else{
				return $content;
			}

		}
		
		public function isError(){
			if($this->_errors){
				return true;
			}else{
				return false;
			}
		}
		
		public function getErrors(){
			return $this->_errors;
		}
		
	}